/******************************************************************************
	Cobalt - utility libraries for building XNA games
	Copyright (C) 2008 Philip Djonov

	This program is free software; you can redistribute it and/or modify it
	under the terms of the GNU General Public License as published by the Free
	Software Foundation; either version 2 of the License, or (at your option)
	any later version.

	This program is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
	for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the
	
		Free Software Foundation, Inc.
		51 Franklin Street, Fifth Floor
		Boston, MA  02110-1301, USA.
******************************************************************************/

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;

namespace Cobalt.Content.Pipeline.Graphics.Font
{

	[ContentTypeWriter]
	internal class FontContentWriter : ContentTypeWriter<SpriteFontContent>
	{
		protected override void Write( ContentWriter output, SpriteFontContent value )
		{
			output.WriteRawObject<int[]>( value.Characters );
			output.WriteRawObject<Texture2DContent[]>( value.ImageSheets );

			output.Write( value.FontPoints.Length );
			foreach( FontPointContent point in value.FontPoints )
			{
				output.Write( point.Size );
				output.Write( point.LineSpacing );
				output.Write( point.Ascent );

				foreach( GlyphContent glyph in point.Glyphs )
				{
					output.Write( Array.IndexOf( value.ImageSheets, glyph.ImageSheet ) );

					output.Write( glyph.DrawOffset.X - 1 );
					output.Write( glyph.DrawOffset.Y - 1 );
					output.Write( (float)glyph.ImageSheetRectangle.Width );
					output.Write( (float)glyph.ImageSheetRectangle.Height );

					output.Write( glyph.ImageSheetRectangle.X );
					output.Write( glyph.ImageSheetRectangle.Y );
					output.Write( glyph.ImageSheetRectangle.Width );
					output.Write( glyph.ImageSheetRectangle.Height );

					output.Write( glyph.AbcKernA );
					output.Write( glyph.AbcKernB );
					output.Write( glyph.AbcKernC );

					char ch = (char)glyph.Character;

					if( char.IsLetter( ch ) )
					{
						char up = char.ToUpper( ch );
						char dn = char.ToLower( ch );

						if( up != ch )
						{
							int idx = Array.BinarySearch<int>( value.Characters, up );
							output.Write( idx != -1 ? idx + 1 : 0 );
						}
						else if( dn != ch )
						{
							int idx = Array.BinarySearch<int>( value.Characters, dn );
							output.Write( idx != -1 ? -(idx + 1) : 0 );
						}
						else
							output.Write( (int)0 );
					}
					else
						output.Write( (int)0 );
				}
			}
		}

		public override string GetRuntimeReader( TargetPlatform targetPlatform )
		{
			return Helpers.GetCobaltFrameworkTypeString( "Graphics.FontReader" );
		}

		public override string GetRuntimeType( TargetPlatform targetPlatform )
		{
			return Helpers.GetCobaltFrameworkTypeString( "Graphics.Font" );
		}
	}

}